<?php

namespace app\cash\model;

use app\beautiful\model\goods\GoodsGroupDetailModel;
use app\beautiful\model\goods\GoodsGroupModel;
use app\beautiful\model\goods\GoodsOtherSkuModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\cash\controller\goods\Cart;
use app\common\model\CommonModel;
use think\facade\Db;


class GoodsModel extends CommonModel{

    protected  $name = 'beautiful_goods';

    protected  $pk   = 'goods_id';

    public static function info($where,$field='*'){
        $json = [];
        if($field=='*'){
            $json[] = 'discount_detail';
        }else{
            $arr = explode(',',$field);
            if(in_array('discount_detail',$arr)){
                $json[] = 'discount_detail';
            }
        }
        $list = self::where($where)->json($json,true)->field($field)->find();
        return $list;
    }

     public static function list($where,$order,$page,$limit,$field='*',$member_id=0){
         $query = Db::name('beautiful_goods')->alias('g')
             ->leftJoin('beautiful_goods_other_sku_detail sd','sd.other_sku_detail_id=g.unit_id')
             ->where(['g.is_delete'=>0])
             ->when(isset($where['cash_category_id']),function ($query) use ($where){
                 $query->where(['g.cash_category_id'=>$where['cash_category_id']]);
             })
             ->when(isset($where['shop_id']),function ($query) use ($where){
                 $query->where(['g.shop_id'=>$where['shop_id']]);
             })
             ->when(isset($where['status']),function ($query) use ($where){
                 $query->where(['g.status'=>$where['status']]);
             })
             ->when(isset($where['origin_type']),function ($query) use ($where){
                 $query->where(['g.origin_type'=>$where['origin_type']]);
             })
             ->when(isset($where['search']),function ($query) use ($where){
                 $query->where('g.name','like','%'.$where['search'].'%');
             })
             ->field($field);
         $count = $query->count();
         $query = $query->order($order);
         if($page>0) $query = $query->page($page);
         if($limit>0) $query = $query->limit($limit);
         $list = $query->field($field)
             ->select()
             ->toArray();
         if(!empty($list)){
             foreach ($list as &$v){
                 //已添加份数
                 $v['cart_num'] = CartModel::num(['goods_id'=>$v['goods_id'],'member_id'=>$member_id,'is_delete'=>0]);

                 $v['group'] = $v['sku'] = [];
                 if($v['type']==3){
                     $re = GoodsGroupModel::list(['goods_id'=>$v['goods_id'],'shop_id'=>$v['shop_id']],['add_time'=>'asc']);
                     $v['group'] = $re['list'];
                 }
                 if($v['is_have_sku']){
                     $field = 'gs.*,sd.group_detail_name as unit_name';
                     $v['sku'] = GoodsSkuModel::list(['goods_id'=>$v['goods_id'],'shop_id'=>$v['shop_id']],['gs.add_time'=>'asc'],0,0,$field);
                 }
                 //个人偏好、制作工艺只需要返回，前端自己匹配
                 $other_sku_id = $v['other_sku_id']?explode(',',$v['other_sku_id']):$v['other_sku_id'];
                 if($other_sku_id)   $ph = GoodsOtherSkuModel::where(['type'=>1])->whereIn('other_sku_id',$other_sku_id)->field('other_sku_id,group_name')->select()->toArray();
                 $v['ph'] = $other_sku_id?$ph:[];
                 if($other_sku_id)   $gy = GoodsOtherSkuModel::where(['type'=>2])->whereIn('other_sku_id',$other_sku_id)->field('other_sku_id,group_name')->select()->toArray();
                 $v['gy'] = $other_sku_id?$gy:[];
             }

         }
         return compact('count','list');
     }
    public static function up($data,$where){
        $data['update_time'] = time();
        $res = self::update($data,$where);
        return $res;

    }

}

